package com.shine.icis.action;

import java.io.IOException;
import java.util.Collection;
import java.util.List;

import com.shine.cotrun.web.comm.HTMLResponse;
import com.shine.esim.struts.webutil.SessionParam;
import com.shine.icis.blo.IFlowService;
import com.shine.icis.config.SysConstants;
import com.shine.icis.dto.EFDept;
import com.shine.icis.dto.EFDeptTree;
import com.shine.icis.dto.EFUser;
import com.shine.icis.hbean.FlowNode;
import com.shine.icis.util.EFRameInf;

public class SubmitFlowConfAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7638121893867812633L;
	
	private IFlowService flowService;
	
	// input param
	private String flowId;
	private String nodeId;
	private String nodeName;//步骤名称
	private String userCode;//授权用户
	private boolean sortUp;//向上排
	private String deptId;//用户部门ID
	
	//output value
	private Collection<FlowNode> flowNodes;
	private Collection<EFUser> efUsers;


	public void setFlowService(IFlowService flowService) {
		this.flowService = flowService;
	}

	public void setFlowId(String flowId) {
		this.flowId = flowId;
	}

	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}

	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	public void setSortUp(boolean sortUp) {
		this.sortUp = sortUp;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public Collection<FlowNode> getFlowNodes() {
		return flowNodes;
	}

	public Collection<EFUser> getEfUsers() {
		return efUsers;
	}

	/**
	 * 进入查询页面
	 * @return
	 */
	public String initQueryFlowNode() {
		return success();
	}
	
	/**
	 * 查询
	 * @return
	 */
	public String queryFlowNode() {
		try {
			flowNodes = flowService.getFlowNodes(flowId);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	/**
	 * 新增
	 * @return
	 */
	public String addFlowNode() {
		if (flowId == null || flowId.trim().equals("")
				|| nodeName == null || nodeName.trim().equals("")
				|| userCode == null || userCode.trim().equals("")) {
			return error("参数不能为空");
		}
		boolean success = false;
		try {
			flowService.addFlowNode(flowId, nodeName, null, new String[]{userCode});
			success = true;
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		} finally {
			SessionParam user = (SessionParam)getSession().getAttribute(SysConstants.SESSION_USER_KEY);
			//记录系统日志
			EFRameInf.saveOpLog(user.getUSER_CODE(), EFRameInf.FUNCCODE_SUBMITFLOW, "增加报送审核步骤，名称[" + nodeName + "]，授权[" + userCode + "]", success);
		}
	}
	
	/**
	 * 修改
	 * @return
	 */
	public String modifyFlowNode() {
		if (nodeId == null || nodeId.trim().equals("")
				|| nodeName == null || nodeName.trim().equals("")
				|| userCode == null || userCode.trim().equals("")) {
			return error("参数不能为空");
		}
		boolean success = false;
		try {
			flowService.modifyFlowNode(nodeId, nodeName, null, new String[]{userCode});
			success = true;
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		} finally {
			SessionParam user = (SessionParam)getSession().getAttribute(SysConstants.SESSION_USER_KEY);
			//记录系统日志
			EFRameInf.saveOpLog(user.getUSER_CODE(), EFRameInf.FUNCCODE_SUBMITFLOW, "修改报送审核步骤，名称[" + nodeName + "]，授权[" + userCode + "]", success);
		}
	}

	/**
	 * 删除
	 * @return
	 */
	public String deleteFlowNode() {
		if (nodeId == null || nodeId.trim().equals("")) {
			return error("参数不能为空");
		}
		boolean success = false;
		try {
			flowService.deleteFlowNode(nodeId);
			success = true;
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		} finally {
			SessionParam user = (SessionParam)getSession().getAttribute(SysConstants.SESSION_USER_KEY);
			//记录系统日志
			EFRameInf.saveOpLog(user.getUSER_CODE(), EFRameInf.FUNCCODE_SUBMITFLOW, "删除报送审核步骤，ID[" + nodeId + "]", success);
		}
	}
	
	public String sortFlowNode() {
		if (nodeId == null || nodeId.trim().equals("")) {
			return error("参数不能为空");
		}
		boolean success = false;
		try {
			flowService.txSortFlowNode(nodeId, sortUp);
			success = true;
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		} finally {
			SessionParam user = (SessionParam)getSession().getAttribute(SysConstants.SESSION_USER_KEY);
			//记录系统日志
			EFRameInf.saveOpLog(user.getUSER_CODE(), EFRameInf.FUNCCODE_SUBMITFLOW, "排序送审核步骤，ID[" + nodeId + "]", success);
		}
	}
	
	/**
	 * 取得下级部门
	 */
	public void loadChildDeptTree() {
		try {
			String depId = EFRameInf.getHeadDepId();
			List<EFDept> depts = EFRameInf.getDepts(depId);
			HTMLResponse.outputHTML(getResponse(), new EFDeptTree(depts, depId).toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 取得部门下的用户
	 * @return
	 */
	public String loadDeptUsers() {
		try {
			SessionParam user = (SessionParam)getSession().getAttribute(SysConstants.SESSION_USER_KEY);
			efUsers = EFRameInf.getUsers(deptId, user.getUSER_CODE(), user.getUSER_GROUP());
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return error();
		}
	}
}
